package com.vichat.common.rudp.thread;

import com.vichat.common.rudp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

public class RudpResponseThread implements Runnable {
    private static transient Logger logger = LoggerFactory.getLogger(RudpResponseThread.class);
    private RudpTransData transData;
    private RudpResponseSrv srvResp;

    public RudpResponseThread(RudpResponseSrv inservice, RudpTransData indata) {
        transData = indata;
        srvResp = inservice;
    }

    public void run() {
        long startIme = System.currentTimeMillis();


        HashMap<Integer, String> respObj = null;
        try {
            ParseData respData = RudpDataUtil.assembelBusiData(transData);
            logger.error(transData.transId + "业务数据组装完成:" + respData.toString());
            try {
                respObj = ProtocolUtil.decodeMap(respData);
                logger.info("!!!!!!!!业务数据转换成功" + respObj.toString());
            } catch (Exception e) {
                logger.error("业务数据转换错误:" + respData.toString(), e);
            }


            //调用计费相关服务
            try {
                if (respObj != null && respObj.containsKey(RudpParamDefine.cmd_param_cmd_type)
                        && respObj.get(RudpParamDefine.cmd_param_cmd_type).equals(String.valueOf(RudpCmdDefine.cmd_type_ack))) {

                    //如果是同步调用的结果，则设置同步调用列表中的结果为返回参数map
                    if (respObj.containsKey(RudpParamDefine.cmd_param_crypt_data)) {

                        if (RudpDataUtil.synreqData.containsKey(respObj.get(RudpParamDefine.cmd_param_crypt_data))) {
                            RudpDataUtil.synreqData.put(respObj.get(RudpParamDefine.cmd_param_crypt_data), respObj);
                            return;
                        }
                    }

                    if(srvResp!=null){
                        srvResp.doResponse(respObj);
                    }


                }
            } catch (Exception e) {
                logger.error("调用接收服务错误" + respObj.toString(), e);

            }
        } catch (Exception e) {
            logger.error(transData.transId + "业务数据组装错误", e);
        }


    }

}

